import PySide6
from PySide6.QtCore import Qt, Signal, QSize
from PySide6.QtWidgets import QDockWidget, QTreeWidget, QTreeWidgetItem


class Dock_Tree(QDockWidget):
    """
    Dock形式的树结构
    1.继承QDockWidget类
    2.父类是主窗口 《重要》 部分关键变量要从父类获取
    3.创建了一个空的QWidget (self.widget) 在QDockWidget上生成树
    4.注意父类的关系是：
            MainWindow
            Dock_Tree(QDockWidget)
            My_TreeWidget(QTreeWidget)
    """

    def __init__(self, parent, config=None):
        super(Dock_Tree, self).__init__(parent)
        self.signal_operat = parent.signal_operat
        self.table_info = parent.table_info
        self.config = config
        if self.config is None:
            self.config = {}

        # 添加dock
        parent.addDockWidget(Qt.LeftDockWidgetArea, self)
        self.setWindowTitle('参数配置')
        self.setContextMenuPolicy(Qt.ContextMenuPolicy.NoContextMenu)
        self.setFeatures(QDockWidget.NoDockWidgetFeatures)
        self.widget = My_TreeWidget(self)
        self.setWidget(self.widget)
        self.load_config()

    # 获取配置文件
    def get_config(self):
        return self.config

    # 加载配置文件
    def load_config(self):
        if self.config:
            if self.config['size']:
                self.resize(QSize(self.config['size'][0], self.config['size'][1]))

    # 重载resize事件，记录dock的大小
    def resizeEvent(self, event:PySide6.QtGui.QResizeEvent) -> None:
        self.config['size'] = [self.width(), self.height()]


class My_TreeWidget(QTreeWidget):
    """ 生成树和节点 """

    def __init__(self,
                 parent=None,
                 config=None,
                 signal=Signal()):
        super(My_TreeWidget, self).__init__(parent)

        # 获取参数
        self.table_info = parent.table_info
        self.signal_operat = parent.signal_operat

        # 配置树
        self.setColumnCount(1)
        self.setHeaderLabels(['文件位置'])
        # self.root = QTreeWidgetItem(self)
        # self.root.setText(0, '系统管理')
        self.setColumnWidth(0, 120)

        # 获取节点
        # self.node_list1 = list(set(self.table_info['type_lv1']))
        # self.node_list1.sort(key=None)
        node_list1 = []
        for i in range(len(self.table_info['category_id'])):
            node_list1.append(self.table_info['category_id'][i][0] + '-' + self.table_info['type_lv1'][i])
        self.node_list1 = list(set(node_list1))
        self.node_list1.sort(key=None)


        for node1 in self.node_list1:
            son_node = QTreeWidgetItem(self)
            son_node.setText(0, node1)
            node_list2 = []
            for i in range(len(self.table_info['type_lv1'])):
                if self.table_info['category_id'][i][0] + '-' + self.table_info['type_lv1'][i] == node1:
                    if self.table_info['category_id'][i] + '-' + self.table_info['type_lv2'][i] not in node_list2:
                        node_list2.append(self.table_info['category_id'][i] + '-' + self.table_info['type_lv2'][i])
                    # if self.table_info['数据类型'][i] in ['5', 'bit'] and self.table_info['选择型操作定义'][i]:
                    #     node_list2.append(self.table_info['2级类型'][i] + '-' + self.table_info['参数名'][i])
            node_list2.sort(key=None)
            for node2 in node_list2:
                grandson_node = QTreeWidgetItem(son_node)
                grandson_node.setText(0, node2)

        # 配置触发事件
        self.doubleClicked.connect(self.tree_double_click)
        self.clicked.connect(self.tree_click)

    def tree_double_click(self):
        item = self.currentItem()
        str_node = item.text(0)
        if str_node in self.node_list1 or str_node == '系统管理':
            pass
        else:
            self.signal_operat.emit(0, 0, [str_node])
            # print(str_node)

    def tree_click(self):
        item = self.currentItem()
        str_node = item.text(0)
        if str_node in self.node_list1 or str_node == '系统管理':
            pass
        else:
            self.signal_operat.emit(0, 1, [str_node])
